package com.gqz.aircraftwar.mvp.model.bomb

import android.graphics.*
import android.graphics.drawable.Drawable
import android.nfc.Tag
import com.gqz.aircraftwar.App
import com.gqz.aircraftwar.App.Companion.paint
import com.gqz.aircraftwar.R
import com.gqz.aircraftwar.util.BmpCache
import com.gqz.aircraftwar.util.i
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 爆炸类
 *
 * @author gqz
 * @data 2020/3/24
 */

class Blast(var x: Float, var y: Float, var isSmall: Boolean) {
    private val TAG: String = "Blast-->"

    private var resId: Int = R.drawable.bomb
    private var bmp: Bitmap

    private var frameW: Float = 0f
    private var frameH: Float = 0f
    private var maxCol = 8
    private var maxRow = 5
    private var index = 0f//当前帧
    private var isPlaying = false

    init {
        bmp = BmpCache.loadBmp(resId)
        if (isSmall) {
            bmp = Bitmap.createScaledBitmap(
                bmp, (bmp.width.toFloat() * 0.5f).toInt(),
                (bmp.height * 0.5f).toInt(), false
            )

        } else {
            bmp = Bitmap.createScaledBitmap(
                bmp, (bmp.width.toFloat() * 1.3f).toInt(),
                (bmp.height * 1.3f).toInt(), false
            )
        }
        frameW = bmp.width.toFloat() / maxCol.toFloat()
        frameH = bmp.height.toFloat() / maxRow.toFloat()
//        num(TAG, "frameW=$frameW , frameH=$frameH")

    }

    fun isPlaying(): Boolean {
        return isPlaying
    }

    fun play() {
        index = 0f
        isPlaying = true
    }

    fun loop() {
        index++
        if (index >= maxRow * maxCol) {
            isPlaying = false
        }
    }

    fun resetPos(pos: PointF) {
        x = pos.x
        y = pos.y
    }

    fun draw(canvas: Canvas) {
        if (isPlaying) {
            var l = (index % (maxCol - 1) * frameW).toInt()
            var t = index.toInt() / (maxCol - 1) * frameH.toInt()
            var bmpRect = Rect(l, t, l + frameW.toInt(), t + frameH.toInt())
            var screenRect = RectF(x, y, x + frameW, y + frameH)
            canvas.drawBitmap(bmp, bmpRect, screenRect, paint)
        }
    }

}
